package com.hzhr.iot.collect.dao;


import com.hzhr.iot.collect.dao.redis.Redis_BaseDao;
import com.hzhr.iot.collect.entity.CachedCommandEntity;
import com.hzhr.iot.collect.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Repository
public class CachedCommandDao extends Redis_BaseDao {
	@Autowired
	RedisUtil redisUtil;
	private String entity = "command";
	@Value("${spring.redis.business.commandPool}")
	private String business;
	
	
	public List<CachedCommandEntity> selectListBySendTime(String sendTime) {
		String key = getKey(business, entity, "sendTime", sendTime);
		return redisUtil.getList(key, CachedCommandEntity.class);
	}
	
	public List<CachedCommandEntity> selectListByDeviceId(String deviceId) {
		String key = getKey(business, entity, "deviceId", deviceId);
		return redisUtil.getList(key, CachedCommandEntity.class);
	}
	
	public void insertBySendTime(CachedCommandEntity commandEntity, String sendTime, long expireTime, TimeUnit timeUnit) {
		String key = getKey(business, entity, "sendTime", sendTime);
		List<CachedCommandEntity> commandEntitieList = selectListBySendTime(sendTime);
		if (commandEntitieList == null) commandEntitieList = new ArrayList<>();
		commandEntitieList.add(commandEntity);
		redisUtil.set(key, commandEntitieList, expireTime, timeUnit);
	}
	
	public void insertListBySendTime(List<CachedCommandEntity> commandEntityList, String sendTime, long expireTime, TimeUnit timeUnit) {
		String key = getKey(business, entity, "sendTime", sendTime);
		List<CachedCommandEntity> oriCommandEntityList = selectListBySendTime(sendTime);
		if (oriCommandEntityList == null) oriCommandEntityList = new ArrayList<>();
		oriCommandEntityList.addAll(commandEntityList);
		redisUtil.set(key, oriCommandEntityList, expireTime, timeUnit);
	}
	
	public void insertBySendTime(CachedCommandEntity commandEntity, String sendTime) {
		String key = getKey(business, entity, "sendTime", sendTime);
		List<CachedCommandEntity> commandEntitieList = selectListBySendTime(sendTime);
		if (commandEntitieList == null) commandEntitieList = new ArrayList<>();
		commandEntitieList.add(commandEntity);
		redisUtil.set(key, commandEntitieList);
	}
	
	public void insertByDeviceId(CachedCommandEntity commandEntity, String deviceId, long expireTime, TimeUnit timeUnit) {
		String key = getKey(business, entity, "deviceId", deviceId);
		List<CachedCommandEntity> commandEntitieList = selectListBySendTime(deviceId);
		if (commandEntitieList == null) commandEntitieList = new ArrayList<>();
		commandEntitieList.add(commandEntity);
		redisUtil.set(key, commandEntitieList, expireTime, timeUnit);
	}
	
	public void insertByDeviceId(CachedCommandEntity commandEntity, String deviceId) {
		String key = getKey(business, entity, "deviceId", deviceId);
		List<CachedCommandEntity> commandEntitieList = selectListByDeviceId(deviceId);
		if (commandEntitieList == null) commandEntitieList = new ArrayList<>();
		commandEntitieList.add(commandEntity);
		redisUtil.set(key, commandEntitieList);
	}
	
	public void setByDeviceId(List<CachedCommandEntity> commandEntityList, String deviceId) {
		String key = getKey(business, entity, "deviceId", deviceId);
		redisUtil.set(key, commandEntityList);
	}
	
	public void deleteByDeviceIdByMessageId(String deviceId, String messageId) {
		String key = getKey(business, entity, "deviceId", deviceId);
		List<CachedCommandEntity> commandEntitieList = selectListByDeviceId(deviceId);
		if (commandEntitieList == null) return;
		for (CachedCommandEntity commandEntity : commandEntitieList) {
			if (messageId.equals(commandEntity.getMessageId())) {
				commandEntitieList.remove(commandEntity);
				break;
			}
		}
		if (commandEntitieList.size() == 0) {
			redisUtil.remove(key);
		} else {
			redisUtil.set(key, commandEntitieList);
		}
	}

	public void deleteByDeviceId(String deviceId) {
		String key = getKey(business, entity, "deviceId", deviceId);
		redisUtil.remove(key);
	}
}
